<?php

namespace app\api\controller\wanlshop;

use app\common\controller\Api;

class Entry extends Api
{
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\index\model\wanlshop\Auth;
        // 获取用户下的申请
        $this->entry = $this->model->where(['user_id' => $this->auth->id])->find();
    }


    public function apply()
    {
        $data = $this->request->post();
        $config = get_addon_config('wanlshop');
        $verify = $config['config']['store_audit'] == 'N' ? 3:2;

        $data['user_id'] = $this->auth->id;
        $data['verify'] = $verify;
        $this->entry ? $this->entry->allowField(true)->save($data) : $this->model->allowField(true)->save($data);
        // 自动审核
        if($config['config']['store_audit'] == 'N'){
            $row = model('app\index\model\wanlshop\Auth')->where(['user_id' => $this->auth->id])->find();
            // 新增店铺
            $shop = model('app\index\model\wanlshop\Shop');
            $shop->user_id = $this->auth->id;
            $shop->state = $row['state'];
            $shop->shopname = $row['shopname'];
            $shop->avatar = $row['avatar'];
            $shop->bio = $row['content'];
            $shop->description = $row['bio'];
            $shop->city = $row['city'];
            $shop->verify = $verify;
            // 新增店铺配置
            if($shop->save()){
                $config = model('app\index\model\wanlshop\ShopConfig');
                $config->shop_id = $shop->id;
                $config->save();
            }
        }
        $this->success();
    }
}